<template>
	<view class="container">
		<view class="wrap ">
			<uni-search-bar placeholder="搜索" @input="search" v-model="word" cancelButton="none" class="sousuokuang">
			</uni-search-bar>
		</view>
		<view class="jiange">

		</view>
		<view class="main">
			<view class="leftnav">
				<view class="navlist" v-for="(item,index) in navList" :key="index">
					<view class="text" :class="item.active?'active':''" @click="navFirst(index)">
						{{item.title}}
					</view>
					<view class="second" v-for="(secItem,secIndex) in item.second" :key="secIndex" v-if="item.active">
						<view class="text" :class="secItem.active?'active':''" @click="navSecond(index,secIndex)">
							{{secItem.title}}
						</view>
					</view>
				</view>
			</view>
			<view class="rightmain">
				<view class="jrjg-div">
					<view class="jrjg-list" v-for="(item,index) in productList" :key="index"
						@click="$public.navto('detail',{productId:item.productId})">
						<image :src="$public.imgageUrl(item.smallImage)" mode="scaleToFill" class="image">
						</image>
						<view class="title1">
							{{item.title}}
						</view>
						<view class="title2">
							<text class="redzi">￥{{item.money}}</text>
							<text style="text-decoration: line-through;margin-left: 15rpx;">￥{{item.marketPrice}}</text>
						</view>
						<view class="title3">
							已售 <text style="color: #333;">{{item.exchangeNum}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				word: "",
				navList: [],
				productList: [],
				bestList: [],
				allProduct: [],
				firstIndex: 0,
				secondIndex: -1,
				columnId: 0
			};
		},
		onLoad(options) {
			if (options.columnId)
				this.columnId = options.columnId
			if (options.word) {
				this.word = decodeURIComponent(options.word)
			}
			this.getMallColumn()
			// this.getProductList()
		},
		onShow() {
			// if (!this.$public.isLoginUrl())
			// 	return false;
		},
		methods: {
			navFirst(index) {
				let i = 0
				this.navList.forEach(item => {
					if (i == index) {
						item.active = true
					} else {
						item.active = false
						item.second.forEach(sitem => sitem.active = false)
					}
					i++
				})
				this.firstIndex = index
				this.secondIndex = -1
				this.productChange()
			},
			navSecond(index, secIndex) {
				let i = 0
				this.navList[index].second.forEach(item => {
					if (i == secIndex) {
						item.active = true
					} else
						item.active = false
					i++
				})
				this.firstIndex = index
				this.secondIndex = secIndex
				this.productChange()


				// console.log(this.navList)
			},
			productChange() {
				// let that=this
				let firstIndex = this.firstIndex
				let secondIndex = this.secondIndex
				let word = this.word
				let firstNav = true;
				let columnId = 0
				let columnArr = []
				this.productList = []
				if (firstIndex == 0) {
					this.productList = this.$_.filter(this.allProduct, item => (item.isBest == 1 && item.title.includes(word)))
					return false
				}
				if (secondIndex >= 0) {
					firstNav = false
					columnId = this.navList[firstIndex].second[secondIndex].columnId
				} else {
					columnArr = this.$_.map(this.navList[firstIndex].second, 'columnId')
					columnArr.push(this.navList[firstIndex].columnId)
				}
				// console.log("firstNav", firstNav)
				if (!firstNav) {
					// console.log("columnId", columnId)
					this.productList = this.$_.filter(this.allProduct, item => (item.columnId == columnId &&
						item.title.includes(word)))
					// console.log("productList", this.productList)
				} else {
					this.productList = this.$_.filter(this.allProduct, item => (this.$_.includes(columnArr, item.columnId) &&
						item.title.includes(word)))
				}
				this.$forceUpdate()

			},
			search(value) {
				console.log("value", value)
				this.word = value
				this.productChange()
			},
			getMallColumn() {
				let that = this
				this.$public.htmlPost("/api/getMallColumn", {
					parentId: 4
				}).then((res) => {
					console.log("getMallColumn res", res)
					let data = res.data;
					if (data.code == 0) {
						this.navList = []
						this.navList = this.$_.filter(data.data, function(o) {
							return o.parentId == 4;
						})
						this.navList.forEach(item => {
							item.active = false
							item.second = that.$_.filter(data.data, function(o) {
								return o.parentId == item.columnId;
							})
							item.second.forEach(sitem => sitem.active = false)
						})
						this.navList.unshift({
							title: "为你推荐",
							columnId: 0,
							second: [],
							active: true
						})
						// console.log("this.navList", this.navList)
						this.getProductList()
					}
				})
			},
			getProductList() {
				let params = {
					orderByColumn: "createTime",
					isAsc: "desc",
					typeId: 4
				}
				// if (this.word)
				// 	params.keyword = this.word
				this.$public.htmlPost("/api/getProductList", params).then((res) => {
					console.log("getProductList res", res)
					let data = res.data;
					if (data.code == 0) {
						data.data.forEach(item => {
							let json = JSON.parse(item.referenceMoney)
							item.marketPrice = json.market_price
							item.money = json.money
							this.allProduct.push(item)
							if (item.isBest == 1)
								this.productList.push(item)
						})
						if (this.columnId > 0)
							this.toColumnNav()
						else {
							this.productChange()
						}
					}
					uni.hideLoading();
				})
			},
			// searchProduct(){
			// 	this.allProduct.forEach(item=>{

			// 	})
			// },
			toColumnNav() {
				if (this.navList.length > 0) {
					let index = this.$_.findIndex(this.navList, item => item.columnId == this.columnId)
					// console.log("index", index)
					this.navFirst(index)
				}
			}

		}
	}
</script>

<style lang="scss" scoped>
	.container {
		margin: 0;
		padding: 0;
	}

	.wrap {
		padding: $uni-wrap-padding;
	}

	.jiange {
		width: 100%;
		height: 1px;
		background: rgba(0, 0, 0, 0.1);
	}

	.main {
		display: flex;
		justify-content: space-between;
	}

	.leftnav {
		width: 200rpx;
		padding: 22rpx 0;

		.first-nav {
			height: 88rpx;
			text-align: center;
			color: #666666;
			font-size: 30rpx;
			text-align: center;
			line-height: 88rpx;
			font-weight: bold;
		}

		.navlist {
			// height: 88rpx;
			text-align: center;
			color: #666666;
			font-size: 30rpx;
			text-align: center;
			line-height: 88rpx;
			font-weight: bold;

			.second {
				background: #FAFAFA;
				color: #333333;
				font-size: 28rpx;
				font-weight: normal;
				// .text {
				// 	color: #333333;
				// 	font-size: 28rpx;
				// 	font-weight: normal;
				// }
			}

			.active {
				color: #DF686A;
			}
		}
	}

	.rightmain {
		background: #F5F5F5;
		padding: 24rpx 15rpx;
		width: 550rpx;
		min-height: 90vh;

		.jrjg-div {
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;

			.jrjg-list {
				width: 250rpx;
				background: #FAFAFA;
				border-radius: 16rpx 16rpx 0 0;
				overflow: hidden;
				font-size: 28rpx;
				margin-bottom: 30rpx;

				.image {
					width: 250rpx;
					height: 250rpx;
				}

				.title1 {
					padding: 0 16rpx;
					color: #333333;
					margin-top: 24rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 3;
					-webkit-box-orient: vertical;
				}

				.title2 {
					padding: 0 16rpx;
					margin: 8rpx 0 16rpx;
					color: #999999;
					font-size: 26rpx;

					.redzi {
						font-size: 32rpx;
					}
				}

				.title3 {
					padding: 0 16rpx;
					color: #999;
					margin-bottom: 16rpx;
					display: flex;
					align-items: center;
					font-size: 26rpx;
				}

				.redzi {
					color: #DF686A;
				}
			}
		}
	}
</style>